<?php

//返回链接中对应的字符
function arg($i=0) {
    if (is_numeric($i)) {
        $m = $_GET['q'];
        if (strpos($m, '/') !== false) {
            $mArr = explode('/', $m);
        }
        return $mArr[$i];
    } else {
        return '';
    }
}

//获取浏览器默认语言
function browserLang() {
    $lang = substr($_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 4); //只取前4位，这样只判断最优先的语言。如果取前5位，可能出现en,zh的情况，影响判断。
    if (preg_match("/zh-c/i", $lang))
        $lan = "zh-cn";
    else if (preg_match("/zh/i", $lang))
        $lan = "zh-tw";
    else if (preg_match("/en/i", $lang))
        $lan = "english";
    else if (preg_match("/fr/i", $lang))
        $lan = "french";
    else if (preg_match("/de/i", $lang))
        $lan = "german";
    else if (preg_match("/jp/i", $lang))
        $lan = "japanese";
    else if (preg_match("/ko/i", $lang))
        $lan = "korean";
    else if (preg_match("/es/i", $lang))
        $lan = "spanish";
    else if (preg_match("/sv/i", $lang))
        $lan = "swedish";
    else
        $lan= $_SERVER["HTTP_ACCEPT_LANGUAGE"];
    return $lan;
}

//语言翻译方法
function Lang($str='') {
    $lang = browserLang();
    if (file_exists(APP_PATH . "lang/$lang.php")) {
        $langArr = include(APP_PATH . "lang/$lang.php");
        if (!is_array($sysConfig))
            return false;
        if (is_null($name))
            return $sysConfig;
        return empty($sysConfig[$str]) ? $str : $sysConfig[$str];
    }
    return $str;
}

//读取html文件
function readHTML($url) {
    $_file_status = is_readable($url);
    if ($_file_status) {
        $_parse_file = file($url);
        # Loop array and load
        for ($i = 0; $i < count($_parse_file); $i++) {
            $_parse_file_str .= $_parse_file[$i];
        }
    }
    return $_parse_file_str;
}

//读取文件
function readFileStream($url) {
    $file = fopen($url, "rb");
    $content = stream_get_contents($file);
    fclose($file);
    return $content;
}

//执行PHP代码
function phpExecute($content) {
    global $db, $config, $common, $html;

    ob_start();
    $content = str_replace('<' . '?php', '<' . '?', $content);
    eval('?' . '>' . trim($content) . '<' . '?');
    $content = ob_get_contents();
    ob_end_clean();
    return $content;
}

// 获取配置值
function C($name=null, $value=null) {
    $sysConfig = $C = array();
    if (file_exists(APP_PATH . "config/config.inc.php")) {
        $sysConfig = include(APP_PATH . "config/config.inc.php");
    } else {
        $sysConfig = array();
    }

    if (file_exists(SYSTEM . "common/config.inc.php")) {
        $sysDefConfig = include(SYSTEM . "common/config.inc.php");
    } else {
        $sysDefConfig = array();
    }

    $diffArr = array_diff_key($sysDefConfig, $sysConfig);
    $C = array_merge($diffArr, $sysConfig);
    if (is_null($name))
        return is_array($C) ? $C : array();
    return isset($C[$name]) ? $C[$name] : null;
}

//import 目录导入
function import($fileName, $fileType='.class.php') {
    $fileName = str_replace('.', '/', $fileName);
    if (file_exists(SYSTEM . "import/$fileName" . $fileType)) {
        require_cache(SYSTEM . "import/$fileName" . $fileType);
        return true;
    }
    return false;
}

function dbExecute($sql) {
    if (empty($sql))
        return false;
    $db = new Csyr();
    if ($num = $db->execute($sql)) {
        return $num;
    } else {
        return false;
    }
}

function dbQuery($sql) {
    if (empty($sql))
        return false;
    $db = new Csyr();
    if ($data = $db->query($sql)) {
        return $data;
    } else {
        return false;
    }
}

//创建数据表
function createContentTypeTable($tableName) {
    $sql = "CREATE TABLE IF NOT EXISTS `" . C("DB_NAME") . "`.`$tableName` ( `id` INT( 11 ) NOT NULL AUTO_INCREMENT ,PRIMARY KEY ( `id` ) ) ENGINE = MYISAM";
    $db = new Csyr();
    if ($db->execute($sql)) {
        return true;
    } else {
        return false;
    }
}

//删除数据表
function delContentTypeTable($table) {
    $sql = "DROP TABLE IF EXISTS `" . C("DB_NAME") . "`.`$table`";
    $db = new Csyr();
    if ($db->execute($sql)) {
        return true;
    } else {
        return false;
    }
}

//增加字段
function addTableField($table, $fieldInfo=array()) {
    if (empty($fieldInfo) || !is_array($fieldInfo))
        return false;
    $field = array('field' => 'pid', 'type' => 'int(11)', 'null' => true);
    if (empty($fieldInfo['field'])) {
        return false;
    } else {
        $fieldName = $fieldInfo['field'];
    }
    if (empty($fieldInfo['type'])) {
        return false;
    } else {
        $type = $fieldInfo['type'];
    }
    if ($fieldInfo['null']) {
        $null = 'NULL';
    } else {
        $null = 'NOT NULL';
    }
    $sql = "ALTER TABLE `$table` ADD `$fieldName` $type $null";
    $db = new Csyr();
    if ($db->execute($sql)) {
        return true;
    } else {
        return false;
    }
}

//删除字段
function delTableField($table, $fieldName) {
    if (empty($table) || empty($fieldName)

        )return false;
    $sql = "ALTER TABLE `$table` DROP `$fieldName`";
    $db = new Csyr();
    if ($db->execute($sql)) {
        return true;
    } else {
        return false;
    }
}

/*
 * 添加节点
 */

function createNode($data=array()) {
    if (!is_array($data))
        return 0;
    if (empty($data))
        return 0;
    if (!isset($data['title']))
        return 0;
    if (!empty($data['content'])) {
        $ncid = @createNodeContent($data['content']);
        is_numeric($ncid) && $data['ncid'] = $ncid;
        unset($data['content']);
    }
    $dbNode = new Csyr("node");
    if ($nid = $dbNode->add($data)) {
        return $nid;
    } else {
        return 0;
    }
}

/*
 * 编辑节点
 */

function editNode($nid, $data=array()) {
    if (!is_array($data))
        return 0;
    if (empty($data))
        return 0;
    if (!isset($data['title']))
        return 0;
    if (!is_numeric($nid) || $nid < 1)
        return 0;

    $dbNode = new Csyr("node");

    if (!empty($data['content'])) {
        $ncid = $dbNode->getField("ncid", "nid=$nid");
        is_numeric($ncid) && $ncNum = editNodeContent($ncid, $data['content']);
        unset($data['content']);
    }

    $num = $dbNode->where("nid=$nid")->save($data);
    if ($ncNum > 0 || $num > 0) {
        return ($ncNum > 0) ? $ncNum : $num;
    } else {
        return 0;
    }
}

/*
 * 删除节点
 */

function delNode($nid) {
    if (!is_numeric($nid) || $nid < 1

        )return 0;
    $dbNode = new Csyr("node");
    if ($num = $dbNode->delete($nid)) {
        return $num;
    } else {
        return 0;
    }
}

/*
 * 获取节点
 */

function getNode($nid=0) {
    if (empty($nid))
        $sql = 'select node.*,nc.*,cate.name as catename from node left join node_content as nc on nc.ncid=node.ncid left join category as cate on cate.cid=node.cid order by node.created desc limit 300';
    else
        $sql='select * from node left join node_content as nc on nc.ncid=node.ncid where nid=' . intval($nid);

    $dbNode = new Csyr("node");
    $node = $dbNode->query($sql);
    if (empty($node)) {
        return array();
    } else {
        return ($nid == 0) ? $node : $node[0];
    }
}

/*
 * 	 添加节点内容(content)
 */

function createNodeContent($content='') {
    if (empty($content))
        return 0;
    $dbNodeContent = new Csyr("node_content");
    if ($ncid = $dbNodeContent->add(array('content' => $content))) {
        return $ncid;
    } else {
        return 0;
    }
}

/*
 * 	 编辑节点内容(content)
 */

function editNodeContent($ncid, $content='') {
    if (!is_numeric($ncid) || $ncid < 1)
        return 0;
    $dbNodeContent = new Csyr("node_content");
    if ($num = $dbNodeContent->where("ncid=$ncid")->save(array('content' => $content))) {
        return $num;
    } else {
        return 0;
    }
}

/*
 * 	 删除节点内容(content)
 */

function delNodeContent($ncid) {
    if (!is_numeric($ncid) || $ncid < 1)
        return 0;
    $dbNodeContent = new Csyr("node_content");
    if ($num = $dbNodeContent->delete($ncid)) {
        return $num;
    } else {
        return 0;
    }
}